package tedu.study.day05;

import java.util.Scanner;

//随机加法运算器
public class Addition {
    public static void main(String[] args) {
        //创建扫描器，接收输入数据
        Scanner scan = new Scanner(System.in);
        //获取一个随机的运算符
        String[] operators ={"+","-","*","/","%"};


        int score = 0;//总分
        int answerYes = 0;//答对数量
        int answerNo = 0;//答错数量
        for(int i=1;i<=10;i++){
            //1)出题 2)答题 3)判题+统计
            int a = (int)(Math.random()*100);//获取第一个数a
            int b = (int)(Math.random()*100);//获取第二个数b
            //获取运算符
            int index = (int)(Math.random()*10);//获取0~9
            String operator = operators[index%5];//获取实际下标0~4中的值
            int result =getResult(a,b,operator);//获取正确答案
            //输出题目
            System.out.println("("+i+")"+a+operator+b+"=?");
            //提示,答题
            System.out.println("请输入运算结果：");
            int answer = scan.nextInt();
            //特殊值表示放弃循环
            if(answer==10086){
                break;
            }
            //判题+统计
            if(answer == result){
                System.out.println("答对了");
                answerYes++;
            }else{
                System.out.println("答错了");
                answerNo++;
            }

        }
        score = answerYes*10;
        System.out.println("答题结果为：答对"+answerYes+"题，答错"+answerNo+"题。总分为："+score);
    }

    //获取随机数和运算符，并返回结果
    public static int getResult(int a,int b,String operator){
        int result = 0;
        if(operator==null){
            System.out.println("运算符错误");
        }else if(operator.equals("+")){
            result=a+b;
        }else if(operator.equals("-")){
            result=a-b;
        }else if(operator.equals("*")){
            result=a*b;
        }else if(operator.equals("/")){
            result=a/b;
        }else if(operator.equals("%")){
            result=a%b;
        }
        return result;
    }
}
